<script type="text/javascript">
	function prepareData() {
		prepareEntrepotInfo();
		prepareProductInfo();
		prepareSpecsDensity();
	}
	
	/*产品信息*/
	function prepareProductInfo() {
		<?php 
		$option = "<option value='dummy'>-</option>";
		foreach ($this->steels as $entry): 
			$option = $option . "<option value='" . $entry->get_id() . "'>". $this->escape($entry->get_name()). "</option>";
		endforeach ?>

		$("select[name|=steel[]]").append($("<?php echo($option); ?>"));
	}

	/*提货地点信息*/
	function prepareEntrepotInfo() {
		<?php $option = ""; ?>
		<?php foreach ($this->entrepots as $entry): 
			echo("g_entrepot[".$this->escape($entry->get_id()) . "]= new EntrepotInfo("
			.$this->escape($entry->get_id()) . ",'"
			.$this->escape($entry->get_name()). "','"
			.$this->escape($entry->get_address()). "','"
			.$this->escape($entry->get_contact()). "','"
			.$this->escape($entry->get_phone()). "');\n");
			$option = $option . "<option value='" . $entry->get_id() . "'>". $this->escape($entry->get_name()). "</option>";
			
		endforeach ?>
		$("select[name|=entrepotId]").append($("<?php echo($option); ?>"));
	}

	/*规格下拉框内容及理重等信息*/
	function prepareSpecsDensity() {
		g_specs['dummy'] = $(g_dummy_option);
		g_specsDensity['dummy' + g_double_underline + 'dummy'] = new SpecsDensityInfo('dummy', 'dummy', 0, 0);
		
		<?php 
		$steelId = null;
		$option = null;
		foreach ($this->specs as $entry): 
		
		echo ("g_specsDensity['" . $entry->get_steelId() . "__" . $entry->get_specs() . "']=new SpecsDensityInfo(" 
				. $this->escape($entry->get_steelId()) . ",'" 
				. $this->escape($entry->get_specs()) . "'," 
				. $this->escape($entry->get_density()) . ");\n");

		
			
			if ($entry->get_steelId() != $steelId) {
				if ($option != null) {
					echo("g_specs[" . $steelId . "]= $(\"" .$option . "\");\n");
				}
				$steelId = $entry->get_steelId();
				$option = "<option value='dummy'>-</option>";
			} 
			$option = $option 
				. "<option value='" 
				. $entry->get_specs()
				. "'>"
				. $this->escape($entry->get_specs())
				. "</option>";
		
		endforeach; 
		echo("g_specs[" . $steelId . "]= $(\"" .$option . "\");\n");
		?>
	}				


	function configEvent() {
		$("select[name|=entrepotId]").change(function(){
			var selected = g_entrepot[$(this).val()];
			$("#address").text(selected.address);
			$("#phone").text(selected.phone);
			$("#contact").text(selected.contact);
		});


		//产品名称和规格下拉框联动
		$("select[name|=steel[]]").change(function(event){
			var obj = event.target;
			var id_appendix = obj.id.split("_")[1];
			
			//empty方法会将所有引用到的地方都清空		
			$("#specs_" + id_appendix).empty();
			//因此需要clone一份
			$("#specs_" + id_appendix).append(g_specs[obj.value].clone());
			
			$("#specs_" + id_appendix).change();
		});
	
	}

	function startUp() {
		$("select[name|=entrepotId]").change();
		$("select[name|=steel[]]").change();
	}
	
</script>


<form enctype="application/x-www-form-urlencoded" method="post" action="/stock/stockin">
<input type="hidden" name="stockinId"/>
<table class="infotable">
<tr>
<td><span class="label">仓库</span><select name="entrepotId"> </select></td>
<td><span class="label">卡号</span><input name="position"/></td>
<td></td>
</tr>
<tr>
<td><span class="label">仓库地址</span><span id="address"/></td>
<td><span class="label">联系人</span><span id="contact"/></td>
<td><span class="label">电话</span><span id="phone"/></td>
</tr>
</table>

<table class="infotable">
    <tr>
     <td class="label" >品名</td>
     <td class="label">规格</td>
     <td class="label">长度</td>
     <td class="label">支数、件数</td>
     <td class="label">重量</td>
     <td class="label">成本价</td>
   </tr>
    <?php for ($i = 1; $i <= 5; $i++) { ?>   
	<tr>	 
     <td>
       <input type="hidden" name="stockId[]" />
       <select name="steel[]" id="steel_<?php echo($i);?>">
       </select>
     </td>
     <td>
     	<select name="specs[]" id="specs_<?php echo($i);?>">
       </select>
     </td>
     <td><input type="text" size="5" name="length[]" id="length_<?php echo($i);?>"></input></td>
     <td><input type="text" size="5" name="amount[]" id="amount_<?php echo($i);?>"></input></td>
     <td><input type="text" size="5" name="weight[]" id="weight_<?php echo($i);?>"></input></td>
     <td><input type="text" size="5" name="costPrice[]" id="costPrice_<?php echo($i);?>"></input></td>
   </tr>
   <?php } ?>    
   </table>


<input type="submit" name="submit" id="submit" value="保存" />
</form>
